package com.ybbase.framework.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ybbase.framework.base.model.TreeModel;
import com.ybbase.framework.base.service.TreeService;
import com.ybbase.framework.model.po.system.Dict;
import com.ybbase.framework.mapper.system.DictMapper;
import com.ybbase.framework.model.po.system.Organization;
import com.ybbase.framework.service.system.DictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 数字字典表 服务实现类
 * </p>
 *
 * @author cg
 * @since 2019-11-25
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    @Autowired
    TreeService treeService;
    @Autowired
    DictService dictService;
    @Autowired
    private DictMapper dictMapper;

    /**
     * 数据字典列表
     *
     * @param list 数据字典列表
     * @return List<TreeModel>
     * @author cg
     * @date 2019-12-01 21:46
     * @since cloud2.0
     */
    @Override
    public List<TreeModel> findSortDict(List<Dict> list) {
        List<TreeModel> firstList = new ArrayList<>();
        List<TreeModel> sourceList = new ArrayList<>();
        for (Dict dict : list) {
            TreeModel treeModel = assemble(dict);
            if (dict.getParentId() == 0) {
                firstList.add(treeModel);
            }
            sourceList.add(treeModel);
        }
        treeService.sortTree(firstList, sourceList);
        return firstList;
    }

    /**
     * 根据key和type查询Dict
     *
     * @param key  关键字
     * @param type
     * @return
     * @author cg
     * @date 2019-12-13 10:24
     * @since cloud2.0
     */
    @Override
    public Dict getDictByKeyType(String key, String type) {
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.lambda().eq(Dict::getDictkey, key);
        dictQueryWrapper.lambda().eq(Dict::getType, type);
        return dictService.getOne(dictQueryWrapper);
    }

    @Override
    public List<Dict> getTree() {
        return dictMapper.getTree();
    }

    @Override
    /**
     * 根据parent_id查找分页数据
     *
     * @return
     */
    public IPage<Dict> findByParentId(IPage<Dict> page, QueryWrapper<Dict> queryWrapper, Integer parentId) {
        return dictMapper.findByParentId(page, queryWrapper, parentId);
    }

    @Override
    public void saveDict(Dict dict) {
        if (dict != null) {
            if (dict.getId() != null && dict.getId() != 0) {
                dictMapper.updateById(dict);
            } else {
                Dict dictData = new Dict();
                String path = "";
                if (dict.getParentId() != null && dict.getParentId() != 0) {
                    dictData = dictMapper.selectById(dict.getParentId());
                    dict.setParentName(dictData.getTypeinfo());
                    path = dictData.getPath();
                } else {
                    dict.setParentId(0);
                }
                dictMapper.insert(dict);
                dict.setPath(path + dict.getId() + "|");
                dictMapper.updateById(dict);
            }
        }
    }

    /**
     * 填充实体
     *
     * @param dict 数据字典实体
     * @return TreeModel 树模型
     * @author cg
     * @date 2019-12-01 22:06
     * @since cloud2.0
     */
    private TreeModel assemble(Dict dict) {
        TreeModel treeModel = new TreeModel();
        treeModel.setId(dict.getId().toString());
        treeModel.setPid(dict.getParentId().toString());
        treeModel.setParentId(dict.getParentId());
        treeModel.setTitle(dict.getDictvalue());
        treeModel.setName(dict.getDictvalue());
        treeModel.setValue(dict.getId().toString());
        treeModel.setSort(dict.getSort());
        Map<String, Object> map = new HashMap<>();
        map.put("code", dict.getDictkey());
        map.put("delFlag", dict.getDelFlag());
        treeModel.setExtendsMap(map);
        return treeModel;
    }
}
